<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>08canvas绘制渐变矩形 </title>
</head>
<style>
    canvas{
        border:1px solid #ccc;
    }
</style>
<body>
<canvas width="600"height="400"></canvas>
<script>
    var myCanvas=document.querySelector('canvas');
    var ctx =myCanvas.getContext('2d');
    /*绘制一个矩形*/
    /*线是由点构成*/
/*    ctx.moveTo(100,100);
    ctx.lineTo(200,100);
    ctx.lineWidth=100;
    ctx.strokeStyle='red';
    ctx.stroke();*/
    ctx.lineWidth=30;
    /*颜色由黑到白线性渐变*/
    for (var i=0;i<255;i++){
        ctx.beginPath();
        ctx.moveTo(99+i,100);
        ctx.lineTo(100+i,100);
        ctx.strokeStyle='rgb('+i+','+i+','+i+')';
        ctx.stroke();
    }
</script>
</body>
</html>
